use graphql_client::{reqwest::post_graphql_blocking as post_graphql, GraphQLQuery};
use serde::Deserialize;
use serde_json::Deserializer;

use self::login_or_reg::UserLoginType;

use super::{get_client, REQ_URL};

#[derive(GraphQLQuery)]
#[graphql(
    query_path = "graphql/query_user.graphql",
    schema_path = "graphql/schema.graphql",
    response_derives = "Serialize,PartialEq"
)]
struct LoginOrReg;

pub fn login_or_reg(cred: String, passwd: String, type_: String) -> bool {
    // use upsert_issue::{IssuesUpdateColumn::*, *};
    // CLIENT.get().unwrap()
    let v = login_or_reg::Variables {
        type_: UserLoginType::deserialize(&mut Deserializer::from_str(&type_[..])).unwrap(),
        cred: cred,
        passwd: passwd,
    };

    let result = post_graphql::<LoginOrReg, _>(get_client(), &REQ_URL[..], v);

    result.is_ok_and(|r| {
        r.data
            .is_some_and(|s| s.user_login.base_query_result.success)
    })
}
